<template>
  <el-button @click="handleClick" type="primary">
    <slot></slot>
  </el-button>
  <div class="choose-icon-dialog-body-height">
    <el-dialog :title="title" v-model="dialogVisible">
      <div class="cntainer">
        <div
          class="item"
          v-for="(item, index) in Object.keys(ElIcon)"
          :key="index"
          @click="clickItem(item)"
        >
          <div class="text"><component :is="item"></component></div>
          <div>{{ item }}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import * as ElIcon from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useCopy } from '../../../hooks/useCopy'
const props = defineProps<{
  title: string // 弹出框的标题
  visible: boolean // 控制弹出框的显示和隐藏
}>()

const emits = defineEmits(['update:visible'])

let dialogVisible = ref<boolean>(props.visible)

const handleClick = () => {
  let val = !props.visible
  dialogVisible.value = val
  emits('update:visible', val)
}

const clickItem = (item: string) => {
  const text = `<el-icon><${item} /></el-icon>`
  useCopy(text)
  dialogVisible.value = false
}

watch(
  () => props.visible,
  (val) => {
    dialogVisible.value = val
  }
)

watch(
  () => dialogVisible.value,
  (val) => {
    emits('update:visible', val)
  }
)
</script>

<style scoped lang="scss">
.cntainer {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.item {
  width: 20%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
  cursor: pointer;
}
.text {
  font-size: 14px;
}

svg {
  width: 2em;
  height: 2em;
}
</style>
